# -*- coding:utf-8 -*-
#from django.db import models
import os
import datetime
import datastore_cache
datastore_cache.DatastoreCachingShim.Install()

from appengine_django.models import BaseModel
from google.appengine.ext import db
from django.utils.encoding import iri_to_uri

#themes = os.listdir(os.path.dirname(__file__) + '/../themes')

class User(BaseModel):
    name = db.StringProperty('用户')
    password = db.BlobProperty('密码')
    google_user = db.UserProperty()

class Blog(BaseModel):
    user = db.ReferenceProperty(User)
    name = db.StringProperty('博客标题',default=u'博客标题')
    description = db.TextProperty('博客描述')
    rss = db.StringProperty('订阅地址',default='/feeds/latest')
    
    #theme = db.StringProperty('主题',choices=themes)
    
    sidebar = db.TextProperty('自定义主页边栏(html)')

class Author(BaseModel):
    name = db.StringProperty('作者',default='Author')
    avatar = db.StringProperty('头像',default='/static/avatars/default.jpg')
    
    description = db.TextProperty('介绍',default=u'更改你的个人说明')
    email = db.StringProperty('Email')

class Tag(BaseModel):
    name = db.StringProperty('名字')
    count = db.IntegerProperty(default=0)
    archives = db.ListProperty(db.Key)

    def get_absolute_url(self):
        return iri_to_uri('/tag/%s' % self.key().name())

class MBlog(BaseModel):
    user = db.ReferenceProperty(User,collection_name='mblogs')
    content = db.StringProperty('内容')
    pub_date = db.DateTimeProperty('时间')

class Category(BaseModel):
    name = db.StringProperty()
    posts = db.IntegerProperty(default=0)

    def query_news(self):
        return self.archives.order('-pub_date')
    def __unicode__(self):
        return self.name + ' - %d' % self.posts
    def get_absolute_url(self):
        return iri_to_uri('/category/%s' % self.key().name())

class Archive(BaseModel):
    title = db.StringProperty('标题',required=True)
    category = db.ReferenceProperty(Category,
            collection_name='archives',
            required=True,
            verbose_name='分类'
            )
    content = db.TextProperty('正文',required=True) 
    view_nums = db.IntegerProperty(default=0)
    pub_date = db.DateTimeProperty( auto_now_add =True)
    update_date = db.DateTimeProperty( auto_now = True )
    tags = db.StringProperty('标签',default=' ')

    def process_tags(self):
        """检查标签"""
        #tags = self.tags.replace(',' , ' ')
        #tags = tags.replace(';' , ' ')
        #self.tags = tags
        #self.put()

        if not self.tags or len(self.tags.lstrip().rstrip()) == 0:
            return
        tags = self.tags.split(',')
        tags = [ tag.rstrip().lstrip() for tag in tags ]
        self.tags = ','.join(tags)
        self.put()

        tag_model = [ Tag.get_or_insert(key_name=tag,name=tag) for tag in tags ]
        tag_model = [tag for tag in tag_model if not self.key() in tag.archives ]
        process = lambda model: model.archives.append(self.key())
        map( process , tag_model )
        map( lambda m: m.put() , tag_model )

    def query_comments(self):
        return self.comments.order('date')
    def get_absolute_url(self):
        return iri_to_uri('/archive/%d' % self.key().id())

class Comment(BaseModel):
    archive = db.ReferenceProperty( Archive ,default=None,collection_name='comments' )
    mblog = db.ReferenceProperty( MBlog ,default=None,collection_name='comments')
    name = db.StringProperty(required=True)
    avatar = db.StringProperty(default='/static/avatars/default.jpg')
    email = db.EmailProperty(required=False)
    web = db.StringProperty()
    source = db.StringProperty()
    content = db.TextProperty(required=True)
    date = db.DateTimeProperty( auto_now_add = True )

    def get_absolute_url(self):
        return self.archive.get_absolute_url()

class Image(BaseModel):
    name = db.StringProperty()
    content_type = db.StringProperty()
    data = db.BlobProperty()

    archive = db.ReferenceProperty(Archive,collection_name='images')
    def get_absolute_url(self):
        return '/image/%s' % str(self.key())



